package com.syh.bishe.service.impl;

import com.syh.bishe.pojo.*;
import com.syh.bishe.mapper.CarbonEmissionsMapper;
import com.syh.bishe.service.ICarbonEmissionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author syh
 * @since 2024-07-01
 */
@Service
@RequiredArgsConstructor
public class CarbonEmissionsServiceImpl extends ServiceImpl<CarbonEmissionsMapper, CarbonEmissions> implements ICarbonEmissionsService {
    private final CarbonEmissionsMapper carbonEmissionsMapper;
    private final CarbonCreditsServiceImpl carbonCreditsService;
    private final LogServiceImpl logService;

    @Override
    public List<CarbonDataWithMonth> getEcharts1(Integer companyId) {
        List<CarbonDataWithMonth> echarts1 = carbonEmissionsMapper.getEcharts1(companyId);
        return echarts1;
    }

    @Override
    public List<CarbonDataWithDay> getEcharts2(Integer companyId) {
        List<CarbonDataWithDay> echarts2 = carbonEmissionsMapper.getEcharts2(companyId);
        return echarts2;
    }

    @Override
    public List<CarbonEmissions> getdata(Integer companyId) {
        List<CarbonEmissions> list = lambdaQuery().eq(CarbonEmissions::getCompanyId, companyId).list();
        return list;
    }

    @Override
    public List<CarbonEmissions> listByStatus() {
        return lambdaQuery().eq(CarbonEmissions::getStatus, "待审核").list();
    }

    @Override
    public DataIndex getTPrat(Integer companyId) {
        List<BigDecimal> tPrat = carbonEmissionsMapper.getTPrat(companyId);
        if (tPrat.size() == 0 || tPrat.size() == 1) {
            return new DataIndex("碳排放", tPrat.size() == 0 ? BigDecimal.valueOf(0.0) : tPrat.get(0), BigDecimal.valueOf(0));
        }
        if (tPrat.get(1).compareTo(BigDecimal.ZERO) == 0) {
            return new DataIndex("碳排放", tPrat.get(0), BigDecimal.valueOf(0));
        }
        BigDecimal difference = tPrat.get(0).subtract(tPrat.get(1));
        return new DataIndex("碳排放", tPrat.get(0), difference.divide(tPrat.get(1), 2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)));
    }

    @Override
    @Transactional
    public Result add(CarbonEmissions carbonEmissions) {
        carbonEmissions.setStatus("待审核");
        try {
            save(carbonEmissions);
            BigDecimal creditAmount = carbonCreditsService.getCreditAmount(carbonEmissions.getCompanyId());
            // 检查新排放量是否大于当前信用额度
            if (carbonEmissions.getEmissionAmount().compareTo(creditAmount) < 0) {
                // 如果新排放量大于信用额度，则更新信用额度
                carbonCreditsService.updateCreditAmount(carbonEmissions.getCompanyId(), carbonEmissions.getEmissionAmount());
                logService.save(new Log().setInfo("ID:" + carbonEmissions.getCompanyId() + "新增碳排放记录," +
                                "排放量" + carbonEmissions.getEmissionAmount() + "吨")
                        .setDate(LocalDateTime.now()).setUserId(carbonEmissions.getCompanyId()));
            } else {
                return Result.error("排放量大于信用额度,请前往购买");
            }
        } catch (Exception e) {
            return Result.error("请输入正确的格式");
        }
        return Result.success();
    }

    @Override
    @Transactional
    public Result update_admin(CarbonEmissions carbonEmissions) {
        if (carbonEmissions.getStatus().equals("驳回")) {
            carbonCreditsService.updateCreditAmountAdd(carbonEmissions.getCompanyId(), carbonEmissions.getEmissionAmount());
        }
        try {
            updateById(carbonEmissions);
            logService.save(new Log().setInfo("审核员审核碳排放记录，审核结果" + carbonEmissions.getStatus())
                    .setDate(LocalDateTime.now()).setUserId(carbonEmissions.getCompanyId()));
        } catch (Exception e) {
            return Result.error("请输入正确的格式");
        }
        return Result.success(carbonEmissions);
    }

    @Override
    @Transactional
    public Result updateTP(CarbonEmissions carbonEmissions) {
        try {
            updateById(carbonEmissions);
            logService.save(new Log().setInfo("ID:" + carbonEmissions.getCompanyId() + "更新碳排放记录," +
                            "排放量" + carbonEmissions.getEmissionAmount() + "吨")
                    .setDate(LocalDateTime.now())
                    .setUserId(carbonEmissions.getCompanyId()));
        } catch (Exception e) {
            return Result.error("请输入正确的格式");
        }
        return Result.success(carbonEmissions);
    }

    @Override
    @Transactional
    public Result delete(Integer id) {
        removeById(id);
        logService.save(new Log().setInfo("ID:" + id + "删除碳排放记录")
                .setDate(LocalDateTime.now()));
        return Result.success();
    }
}
